import http from '@ohos.net.http';

let BASE_URL = "https://localhost:8080"

export async function sendCode(phoneNumber: string) {
    let httpRequest = http.createHttp()
    return new Promise((resolve => {
        httpRequest.request(`${BASE_URL}/code`, {
            method: http.RequestMethod.POST,
            header: {
                'Content-Type': 'application/json'
            },
            // 当使用POST请求时此字段用于传递内容
            extraData: {
                "phoneNumber": phoneNumber,
            },
            connectTimeout: 60000, // 可选，默认为60000ms
            readTimeout: 60000, // 可选，默认为60000ms
        }, (err, data) => {
            if (!err) {
                resolve(data)
            }
        })
    }))
}

export async function verifyCode(phoneNumber: string, code: string): Promise<boolean> {
    let httpRequest = http.createHttp()
    return new Promise((resolve => {
        httpRequest.request(`${BASE_URL}/code`, {
            method: http.RequestMethod.POST,
            header: {
                'Content-Type': 'application/json'
            },
            // 当使用POST请求时此字段用于传递内容
            extraData: {
                "phoneNumber": phoneNumber,
                "code": code
            },
            connectTimeout: 60000, // 可选，默认为60000ms
            readTimeout: 60000, // 可选，默认为60000ms
        }, (err, data) => {
            resolve(!err)
        })
    }))
}